<?php

class Manga_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    function get_mangas_list($user_id, $reading_mangas = '') {

        //If first time do the test, get all mangas for user to read, else get current mangas user is reading

        $number_manga = 4; //Number of mangas for user to read. Make changes here        

        $query_str = "SELECT ml.manga_id,ml.title,ml.total_pages,mi.full_path
                      FROM manga_lists ml, manga_images mi
                      WHERE ml.manga_id = mi.manga_id
                      AND mi.page_num = 0 ";

        if (empty($reading_mangas)) {

            $query_str .= "ORDER BY RAND() LIMIT $number_manga";
            $data = $this->db->query($query_str);

            //Serialize list of mangas and store it into database

            $all_questions = array();
            foreach ($data->result() as $row) {
                array_push($all_questions, $row->manga_id);
            }

            $serialize = serialize($all_questions);

            $query_str = "UPDATE test_results SET question_list = ? WHERE user_id = ?";
            $this->db->query($query_str, array($serialize, $user_id));
        } else {

            $unserialized = unserialize($reading_mangas);
            $mangas_list = implode(',', $unserialized);

            $query_str .= "AND ml.manga_id IN ($mangas_list)";
            $data = $this->db->query($query_str);
        }

        return $data;
    }

    function get_reading_mangas($user_id) {

        $query_str = "SELECT question_list FROM test_results WHERE user_id = ?";
        $result = $this->db->query($query_str, $user_id);

        $reading_mangas = $result->row(0)->question_list;

        return $reading_mangas;
    }

    function get_test_result($user_id) {
        //Get score from database, pick the 3 largest
        $query_str = "SELECT conventional,artistic,investigate,social,enterprising,realistic FROM test_results WHERE user_id = ?";

        $result = $this->db->query($query_str, $user_id)->row_array();

        //Sort $result in highest to lowest
        arsort($result);
        $output = array();

        //Pick the 3 highest        
        foreach ($result as $key => $value) {
            if (count($output) < 3) {
                $key = "'" . $key . "'"; //Add single quote surround personality for query in IN() statement database
                array_push($output, $key);
            }
        }
        //Make $output as a string for query
        $result_string = implode(',', $output);

        //Query personality type from database
        $query_str = "SELECT * FROM personality WHERE personality_type IN ($result_string)";

        return $this->db->query($query_str);
    }

    function get_page($manga_id, $page_num) {
        $query_str = "SELECT page_num, full_path FROM manga_images WHERE manga_id = ? AND page_num = ?";

        return $this->db->query($query_str, array($manga_id, $page_num));
    }

    function update_score($user_id, $character_id) {

        //Get personality from character    
        $query_str = "SELECT personality_type FROM characters WHERE character_id = ?";
        $personality = $this->db->query($query_str, $character_id)->row(0)->personality_type;

        //Update score
        $query_str = "UPDATE test_results SET $personality = $personality+1 WHERE user_id = ?;";
        $this->db->query($query_str, $user_id);
    }

    function remove_manga($user_id, $manga_id) {

        $query_str = "SELECT question_list FROM test_results WHERE user_id = ?";
        $question_list = $this->db->query($query_str, $user_id)->row(0)->question_list; //Current quetions lists (serialized)

        $question_list_arr = unserialize($question_list);

        //If found manga_id in array, remove it, serialize again and UPDATE database
        $manga_id_arr = array($manga_id);
        $new_array = array_diff($question_list_arr, $manga_id_arr);

        //serialize again
        if (count($new_array) === 0) {
            $serialized = 'done';
        } else {
            $serialized = serialize($new_array);
        }

        //Update database
        $query_str = "UPDATE test_results SET question_list = ? WHERE user_id = ?";
        $this->db->query($query_str, array($serialized, $user_id));
    }

}

/* End of file manga_model.php */
/* Location: ./application/models/manga_model.php */

